<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="gv-sub-content">
  <div class="gv-forms role" layout="column">
    <div class="gv-forms-header">
      <h1 ng-if="!$ctrl.editMode">New role [{{$ctrl.role.scope}}]</h1>
      <h1 ng-if="$ctrl.editMode">Update [{{$ctrl.role.scope}}]: {{::$ctrl.role.name}}</h1>
      <a ui-sref="organization.settings.roles">Back to roles</a>
    </div>

    <form name="$ctrl.formRole" ng-submit="$ctrl.save()" novalidate>
      <div class="gv-form">
        <h2>Details</h2>
        <div class="gv-form-content" layout="column">
          <md-input-container ng-if="!$ctrl.editMode">
            <label>Role name</label>
            <input ng-model="$ctrl.role.name" required autofocus />
          </md-input-container>

          <md-input-container>
            <label>Role description</label>
            <input ng-model="$ctrl.role.description" />
          </md-input-container>

          <md-input-container>
            <md-checkbox ng-model="$ctrl.role.default"> Default role </md-checkbox>
          </md-input-container>

          <md-table-container>
            <table md-table class="gv-table-dense">
              <thead md-head>
                <tr md-row>
                  <th md-column></th>
                  <th md-column>CREATE</th>
                  <th md-column>READ</th>
                  <th md-column>UPDATE</th>
                  <th md-column>DELETE</th>
                </tr>
                <tr md-row>
                  <th md-cell></th>
                  <th md-cell>
                    <md-checkbox
                      aria-label="Select all create"
                      ng-model="$ctrl.createCheckedAll"
                      ng-change="$ctrl.selectAll('C', $ctrl.createCheckedAll)"
                    ></md-checkbox>
                  </th>
                  <th md-cell>
                    <md-checkbox
                      aria-label="Select all read"
                      ng-model="$ctrl.readCheckedAll"
                      ng-change="$ctrl.selectAll('R', $ctrl.readCheckedAll)"
                    ></md-checkbox>
                  </th>
                  <th md-cell>
                    <md-checkbox
                      aria-label="Select all update"
                      ng-model="$ctrl.updateCheckedAll"
                      ng-change="$ctrl.selectAll('U', $ctrl.updateCheckedAll)"
                    ></md-checkbox>
                  </th>
                  <th md-cell>
                    <md-checkbox
                      aria-label="Select all delete"
                      ng-model="$ctrl.deleteCheckedAll"
                      ng-change="$ctrl.selectAll('D', $ctrl.deleteCheckedAll)"
                    ></md-checkbox>
                  </th>
                </tr>
              </thead>
              <tbody md-body>
                <tr md-row ng-repeat="permission in $ctrl.permissions">
                  <td md-cell>
                    <div>
                      <b>{{permission}}</b>
                    </div>
                    <div class="permission-moved" ng-if="$ctrl.isEnvironmentTagOrTenant(permission)">
                      This permission has been moved to ORGANIZATION scope
                    </div>
                  </td>
                  <td md-cell>
                    <md-checkbox
                      ng-disabled="$ctrl.isEnvironmentTagOrTenant(permission)"
                      aria-label="{{permission}} create"
                      ng-model="$ctrl.role.permissions[permission].C"
                      ng-change="$ctrl.checkSelectAll()"
                    >
                    </md-checkbox>
                  </td>
                  <td md-cell>
                    <md-checkbox
                      ng-disabled="$ctrl.isEnvironmentTagOrTenant(permission)"
                      aria-label="{{permission}} read"
                      ng-model="$ctrl.role.permissions[permission].R"
                      ng-change="$ctrl.checkSelectAll()"
                    ></md-checkbox>
                  </td>
                  <td md-cell>
                    <md-checkbox
                      ng-disabled="$ctrl.isEnvironmentTagOrTenant(permission)"
                      aria-label="{{permission}} update"
                      ng-model="$ctrl.role.permissions[permission].U"
                      ng-change="$ctrl.checkSelectAll()"
                    ></md-checkbox>
                  </td>
                  <td md-cell>
                    <md-checkbox
                      ng-disabled="$ctrl.isEnvironmentTagOrTenant(permission)"
                      aria-label="{{permission}} delete"
                      ng-model="$ctrl.role.permissions[permission].D"
                      ng-change="$ctrl.checkSelectAll()"
                    ></md-checkbox>
                  </td>
                </tr>
              </tbody>
            </table>
          </md-table-container>

          <md-card-actions layout="row">
            <md-button
              type="submit"
              ng-if="!$ctrl.role.system"
              ng-disabled="$ctrl.formRole.$invalid || $ctrl.formRole.$pristine"
              class="md-raised md-primary"
            >
              Save</md-button
            >
          </md-card-actions>
        </div>
      </div>
    </form>
  </div>
</div>
